﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardButton : MonoBehaviour {
    EginMahjong mahjong = EginMahjong.Instance;
    public bool TempBool = false; //控制上下
    private bool canPut;
    private bool isDoubleClick;
    private float TimeOne;
    private float TimeTwo;
    private Vector2 lastPosition;
    private bool isDrag = false;
    private bool canMove = false;
    public void OnClickButton()
    {
        ClickCardEvent();
    }

    private void Update()
    {
        if (canMove)
        {
            transform.Translate(0, 0.01f, 0);
            if (transform.localPosition.y > (-304 + 20))
            {
                CardModel.SendMahjongPlayRequest(int.Parse(this.gameObject.name));
                mahjong.handCard = 0;
                canMove = false;
            }
        }
    }


    void ClickCardEvent()
    {
        //判断是否是双击
        TimeTwo = Time.realtimeSinceStartup;
        if (TimeTwo - TimeOne < 0.25)
        {
            isDoubleClick = true;
            Debug.Log("双击");
        }
        else
        {
            isDoubleClick = false;
        }
        TimeOne = TimeTwo;
        //判断是不是轮到本人出牌
        if ((mahjong.handCard != 0 || mahjong.nextSid == mahjong.seatID) && !mahjong.havaSpecailCard)
        {
            canPut = true;

        }
        else
        {
            canPut = false;
        }
        if (isDoubleClick)//&& canPut 测试不加 不测试要加上
        {
            if (canPut)
            {
                //发送出牌信息SendMahjongPlayRequest();
                CardModel.SendMahjongPlayRequest(int.Parse(this.gameObject.name));
                Debug.Log("出牌");
                mahjong.handCard = 0;
            }
        }
        else
        {
            if (!TempBool)
            {
                gameObject.transform.localPosition = new Vector3(gameObject.transform.localPosition.x,
                                                                 gameObject.transform.localPosition.y + 20,
                                                                 gameObject.transform.localPosition.z);
                TempBool = true;
            }
            else if (TempBool)
            {
                if (canPut)
                {
                    //发送出牌信息SendMahjongPlayRequest();
                    CardModel.SendMahjongPlayRequest(int.Parse(this.gameObject.name));
                    Debug.Log("出牌");
                    mahjong.handCard = 0;
                }
                else
                {
                    gameObject.transform.localPosition = new Vector3(gameObject.transform.localPosition.x,
                                                                gameObject.transform.localPosition.y - 20,
                                                                gameObject.transform.localPosition.z);
                    TempBool = false;
                }
            }

        }

    }

    void OnDrag(Vector2 delta)
    {
        if (enabled && NGUITools.GetActive(gameObject))
        {
            if (mahjong.handCard != 0 || mahjong.nextSid == mahjong.seatID)
            {
                canPut = true;

            }
            else
            {
                canPut = false;
            }
            if (canPut && delta.y < lastPosition.y && lastPosition.y != 0)
            {
                canMove = true;
                //CardModel.SendMahjongPlayRequest(int.Parse(this.gameObject.name));
                //user.handCard = 0;
            }

            lastPosition = delta;
        }
    }
}
